package tech.aistar.controller.daoyou;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.aistar.config.GuidelInterceptor;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.daoyou.*;
import tech.aistar.entity.person_center.Orders;
import tech.aistar.service.daoyou.GuideService;
import tech.aistar.util.SmsModel;
import tech.aistar.util.date_SimpleDateFormat.DateUtileDemo;
import tech.aistar.vo.daoyou.GuideOrdersAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 导游的个人中心
 */
@Controller
@RequestMapping("/guidehome")
public class GuideHomepage {

    @Autowired
    GuideService service;



// ===
    @GetMapping("/home")
    @GuidelInterceptor
    /**
     * 用户登陆以后 获取到session作用域中userTable 的id
     * 然后拿id 去导游申请表中 判断是否实名 如果实名那么返回guideInfo对象
     * 如果没有 则返回guideInfo是null
     * 通过返回的guideInfo对象 来获取所有的订单
     *
     */

    public String Guideloggin(HttpServletRequest request){
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");
        //通过用户得id 去查找出是否申请了导游是否通过 如果通过 在拿着guide_apply 得id 去找GuideInfo 中找主键
        //主键才是核心

        GuideInfo guideInfo= service.SelectGuideById(userTable.getId());
        if(guideInfo==null){
            //不是导游 应该跳转到注册导游得页面
            System.out.println("不是导游");
            return "redirect:/applytourist";
        }else if(guideInfo.getStatus()==-2){
            System.out.println("导游下架");
//说明该导游已经被下架了
            return "redirect:/guide";
        }else {
            //说明该导游合法
            //查订单消息
            session.setAttribute("daoyou_id",guideInfo.getId());
            Integer id= guideInfo.getId();
            List<GuideOrdersAll> ordersbill = getordersBill(id);
            request.setAttribute("bill",ordersbill);
            GuideRoute TEXT= service.selectRouteByid(id);
            request.setAttribute("texts",TEXT);
            GuideInfo guideInfo1=service.getGuideById(id);

            request.setAttribute("start",guideInfo1);
            Integer nums=service.getPhoneNum(id);
            if(nums==null)
                nums=0;
            request.setAttribute("num",nums+1);


            return "/Tourist/myTraveler";
        }

    }



    //获取导游得请求
    //一个导游在一段时间内 只能接收到一个任务
    @PostMapping("/getbill/{id}")
    @GuidelInterceptor
    public @ResponseBody GuideResult getBill(@PathVariable("id") Integer id,HttpServletRequest request){
        HttpSession session= request.getSession();
        Integer ids= (Integer) session.getAttribute("daoyou_id");
    //先检测该订单是否可以接
       // service.checkOrder(id);
        //判断可不可以接单 然后再更改记录
     //   service.accessBill(id); 导游接单
//        service.getOrderById(guide_id,applyTime,sqlend);
        Orders orders=service.getBillById(id);
        String starttime=DateUtileDemo.format("yyyy-MM-dd",orders.getApplyTime());
        String endtime=DateUtileDemo.format("yyyy-MM-dd",orders.getPlayDay());
        java.sql.Date sqlend= java.sql.Date.valueOf(starttime);
        Boolean flag= service.getOrderById(ids, starttime,sqlend);
        if(flag==true){
               service.accessBill(id);
               //短信通知
            SmsModel.sendSms(orders.getPhoneNumber(),441664,orders.getApplyName());
            return new GuideResult(GuideCodeStatus.OK,true,"ok");
        }else{
            return new GuideResult(GuideCodeStatus.OK,false,"接单时间冲突");
        }


    }

    //拒绝
    //需要短信通知用
    @PostMapping("/delbill/{id}")
    @GuidelInterceptor
    public @ResponseBody GuideResult delBill(@PathVariable("id") Integer id,HttpServletRequest request){

        service.delBill(id);
      Orders orders=service.getBillById(id);
//  拒绝短信
        SmsModel.sendSms(orders.getPhoneNumber(),441800,orders.getApplyName());


        return new GuideResult(GuideCodeStatus.OK,true,"成功");
    }




    //查看导游的接单情况
    @PostMapping("/getBeing")
    @GuidelInterceptor
    public @ResponseBody GuideResult GuideSelectBill(HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer ids= (Integer) session.getAttribute("daoyou_id");
        List<GuideOrdersAll> list=service.getAllBill(ids);
       return  new GuideResult(GuideCodeStatus.OK,true,"成功",list);
    }


    //路线业务
    @PostMapping("/getRout")
    @GuidelInterceptor
public  @ResponseBody GuideResult GuideRout(@Validated GuideRoute guideRoute, HttpServletRequest request){
    HttpSession session=request.getSession();
    Integer ids= (Integer) session.getAttribute("daoyou_id");
    String text= guideRoute.getGuideText();
try{
    service.guideRout(ids,text);
    return new GuideResult(GuideCodeStatus.OK,true,"成功");
}catch (Exception e){
    return new GuideResult(GuideCodeStatus.ERROR,false,"系统错误");
}

}
//更改自己的状态
    @PostMapping("/setStart/{start}")
    @GuidelInterceptor
    public @ResponseBody GuideResult setStart(@PathVariable("start") Integer start,HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer ids= (Integer) session.getAttribute("daoyou_id");
        try{
            service.setGuideById(ids,start);
            return new GuideResult(GuideCodeStatus.OK,true,"成功");
        }catch (Exception e){
            return new GuideResult(GuideCodeStatus.ERROR,false,"系统错误");
        }

    }


    //导游退订单

    /**
     * 要短信通知用户 还要扣除相应的分值
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/setOrders")
    @GuidelInterceptor
    public @ResponseBody GuideResult setOrders(Integer id,HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer ids= (Integer) session.getAttribute("daoyou_id");
        try{
            service.setOrders(id,ids);
            return new GuideResult(GuideCodeStatus.OK,true,"成功");
        }catch (Exception e){
            return new GuideResult(GuideCodeStatus.ERROR,false,"系统错误");
        }

    }


/*
点赞
 */
    @RequestMapping ("/addPraise")
    public @ResponseBody GuideResult addPraise(Integer id,HttpServletRequest ip){
        String addres= ip.getRemoteAddr();
        String  num= service.addPRAISE(id,addres);
        if( "no".equals(num)) {
            return new GuideResult(GuideCodeStatus.ERROR,false,"今天您已经点过啦!");
        }else{
            return new GuideResult(GuideCodeStatus.ERROR,true,"感谢投票",num);
        }
    }


    /*

     */
    @RequestMapping ("/addip")
//    @GuidelInterceptor
    public @ResponseBody GuideResult addIp(HttpServletRequest ip){
//       String addres= ip.getRemoteAddr();
//       String host=ip.getRemoteHost();
//       System.out.println(addres+ " "+host);
        service.delRedis();

            return new GuideResult(GuideCodeStatus.ERROR,true,"");

    }



    //获取导游的模板
    public List<GuideOrdersAll> getordersBill(Integer id){
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd ";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String  ordertime= sdf.format(date);
        //获取了所有得申请信息
        List<GuideOrdersAll> ordersbill=service.getBill(id,ordertime);
        return  ordersbill;
    }






}
